//
//  SSNormalDetailController.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/5/26.
//

import UIKit

import SwiftyJSON

//“1776CA”背景色

class SSNormalCleanViewController: UIViewController {
    @IBOutlet weak var segmentView: SSSegmentView!
    @IBOutlet weak var tableview: UITableView!
    
    var monthM = SSNormal()
    var singleM = SSNormal()
    var yearM = SSNormal()
    var petM = SSNormal()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "日式清洁"
        
        network()
        
        segmentView.setupUI()
        
        segmentView.mealResult { index in
            print("---:\(index)")
            self.tableview.selectRow(at: IndexPath(row: 0, section: index), animated: true, scrollPosition: .top)
        }
//        setupUIS()

    }

    func setupUIS() {
        
        let footerView = SSNormalFooterView.footerView()
        footerView.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: 870)
        tableview.tableFooterView = footerView
        tableview.separatorStyle = .none
        tableview.bounces = false
        tableview.delegate = self
        tableview.dataSource = self
        tableview.backgroundColor = UIColor(hex: "1776CA")
//        tableview.backgroundColor = UIColor.red
        tableview.register(UINib(nibName: "SSNormalTableViewCell", bundle: Bundle.init(for: SSNormalTableViewCell.self)), forCellReuseIdentifier: "SSNormalTableViewCell")
        tableview.register(UINib(nibName: "SSNormalMealCell", bundle: Bundle.init(for: SSNormalMealCell.self)), forCellReuseIdentifier: "SSNormalMealCell")
    }


}

extension SSNormalCleanViewController: UITableViewDelegate, UITableViewDataSource
{
    func setupContentOffset(y:Double) {
        print("----contentOffset.y:\(y)")
        var index = 0
        if(y < 1158){
            index = 0
        }
        if(1158 < y && y < 2140){
            index = 1
        }
        if(2140 < y && y < 3515){
            index = 2
        }
        if(y > 3515){
            index = 3
        }
        self.segmentView.setupSelectedItem(index: index)
    }
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        setupContentOffset(y: scrollView.contentOffset.y)
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        setupContentOffset(y: scrollView.contentOffset.y)
    }


    func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if (section == 0){
            return 704
        }else if (section == 1){
            return (706 - 215 + 40 + 6)
        }else if (section == 2){
            return 174 - 20
        }else if (section == 3){
            return (706 - 215 + 40 + 6)
        }
        return 706
    }
    
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if(section == 0){
            return monthM.listPackage.count - 1
        }
        if(section == 1){
            return singleM.listPackage.count - 1
        }
        if(section == 2){
            return yearM.listPackage.count
        }
        if(section == 3){
            return petM.listPackage.count - 1
        }
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if(indexPath.section == 2){
            let cell = tableView.dequeueReusableCell(withIdentifier: "SSNormalMealCell") as! SSNormalMealCell
            cell.cancelCorner()
            if(indexPath.row == self.yearM.listPackage.count - 1){
                cell.setCorner()
            }
            cell.normal = self.yearM.listPackage[indexPath.row]
            return cell
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "SSNormalTableViewCell") as! SSNormalTableViewCell
        if(indexPath.section == 0){
            cell.normal = self.monthM.listPackage[indexPath.row + 1]
            cell.cancelCorner()
            if(indexPath.row == self.monthM.listPackage.count - 2){
                cell.setCorner()
            }
        }
        if(indexPath.section == 1){
            cell.normal = self.singleM.listPackage[indexPath.row + 1]
            cell.cancelCorner()
            if(indexPath.row == self.singleM.listPackage.count - 2){
                cell.setCorner()
            }
        }
        if(indexPath.section == 3){
            cell.normal = self.petM.listPackage[indexPath.row + 1]
            cell.cancelCorner()
            if(indexPath.row == self.petM.listPackage.count - 2){
                cell.setCorner()
            }
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if(indexPath.section == 2){
            return 406
        }
        return 150
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        if(section == 0){
            let view = SSNormalSectionHeader.headerView()
            view.mainTitleLab.isHidden = false
            view.headerBG.isHidden = false
            view.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: 704)
            view.whiteCardTopConstent.constant = 215
            view.monthM = self.monthM
            return view
        }else if (section == 1){
            let view = SSNormalSectionHeader.headerView()
            view.mainTitleLab.isHidden = true
            view.headerBG.isHidden = true
            view.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: 706 - 215 + 40 + 6)
            view.monthM = self.singleM
            view.whiteCardTopConstent.constant = 52
            return view
        }else if (section == 2){
            let view = SSNormalSectionHeader2.headerView()
            view.normal = self.yearM
            view.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: 174 - 20)
            return view
        }else if (section == 3){
            let view = SSNormalSectionHeader.headerView()
            view.mainTitleLab.isHidden = true
            view.headerBG.isHidden = true
            view.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: 706 - 215 + 40 + 6)
            view.monthM = self.petM
            view.whiteCardTopConstent.constant = 52
            return view
        }
        
        return UIView()
    }
}

extension SSNormalCleanViewController{
    func network() {
        let param = [
            "parentId":"25"
        ] as [String:AnyObject]
        
        SSNetwrokManager.share.request(urlString: APIUrl.share.getDayItem_URL, parameters:param) { result in
            let json = JSON(parseJSON: result!)
            let data = json["data"].dictionary
            
            let listMonthsDic = data!["monthItem"]?.dictionary
            let listSingleDic = data!["singleItem"]?.dictionary
            let listYearsDic = data!["yearItem"]?.dictionary
            let listPetsDic = data!["petItem"]?.dictionary
            let single = self.parseData(listMonthsDic: JSON(rawValue: listSingleDic!) ?? JSON(),type: 0)
            let month = self.parseData(listMonthsDic: JSON(rawValue: listMonthsDic!) ?? JSON(),type: 1)
            let year = self.parseData(listMonthsDic: JSON(rawValue: listYearsDic!) ?? JSON(),type: 1)
            let pet = self.parseData(listMonthsDic: JSON(rawValue: listPetsDic!) ?? JSON(),type: 2)
            self.monthM = month
            self.singleM = single
            self.yearM = year
            self.petM = pet
            debugPrint("")
            self.setupUIS()
            self.tableview.reloadData()
        }
    }
    
    
    func parseData(listMonthsDic:JSON, type:Int = 0) -> SSNormal {
        let listMonth = SSNormal()
        listMonth.title = listMonthsDic["title"].stringValue
        listMonth.describe = listMonthsDic["describe"].stringValue
        listMonth.imgUrl = listMonthsDic["imgUrl"].stringValue
        //数据list
        let listPackages = listMonthsDic["listPackage"].arrayValue
        for(_, item) in listPackages.enumerated(){
            let package = SSNormal()
            package.pkgName = item["pkgName"].stringValue
            package.keyword = item["keyword"].stringValue
            package.pkgImg = item["pkgImg"].stringValue
            package.pkgType = item["pkgType"].stringValue
            package.pkgId = item["pkgId"].stringValue
            package.couponText = item["couponText"].stringValue
            package.type = type

            //标签
            let listTag = item["listTag"].arrayValue
            for(_, subItem) in listTag.enumerated(){
                let tag:String = subItem["tagName"].stringValue
                package.listTag.append(tag)
            }
            
            //轮播图
            let listSwiperImg = item["listSwiperImg"].arrayValue
            for subItem in listSwiperImg {
                let specs:SSCycleModel = SSCycleModel()
                specs.imgUrl = subItem["imgUrl"].stringValue
                package.listSwiperImg.append(specs)
            }
            
            
            //规格
            let listSpecs = item["listSpecs"].arrayValue
            for(index, subItem) in listSpecs.enumerated(){
                let specs:SSNormal = SSNormal()
                specs.specsName = subItem["specsName"].stringValue
                specs.specsPrice = subItem["specsPrice"].floatValue
                specs.specsDescribe = subItem["specsDescribe"].stringValue
                specs.specsId = subItem["specsId"].stringValue


                
                if(index == 0){specs.isSelected = true}
                package.listSpecs.append(specs)
            }
            listMonth.listPackage.append(package)
        }
        return listMonth
    }
    
}
